package com.lxit.video.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.lxit.video.bean.UserInfo;
import com.lxit.video.dao.UserDao;

public class UserRepositoryImpl implements UserDao {
	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public List<UserInfo> pageGetAll(Integer pageIndex, Integer pageSize, String name) {
		StringBuffer sql = new StringBuffer("from UserInfo u where 1=1 ");
		if (name != null && name != "") {
			sql.append("and u.userAlias like '%" + name + "%' ");
		}

		Query query = entityManager.createQuery(sql.toString());

		query.setFirstResult((pageIndex - 1) * pageSize);
		query.setMaxResults(pageSize);
		return query.getResultList();
	}

	@Override
	public int countAll(String name) {
		StringBuffer sql = new StringBuffer("select count(1) from UserInfo u where 1=1 ");
		if (name != null && name != "") {
			sql.append("and u.userAlias like '%" + name + "%' ");
		}
		Query query = entityManager.createQuery(sql.toString());
		return Integer.parseInt(query.getSingleResult().toString());
	}

}
